package com.tinem.platform.web.tool.config;

import com.tinem.platform.web.tool.filter.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Date;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className WebConfig
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-03 14:50
 */
@Configuration
@SuppressWarnings("unchecked")
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(String.class, Date.class,(value)->new Date(Long.parseLong(value)));
        registry.addConverter(Date.class, String.class,(value)->String.valueOf(value.getTime()));
    }

    @Bean
    public FilterRegistrationBean gatewayFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(2);
        filterRegistrationBean.setFilter(new CorsFilter());
        filterRegistrationBean.setName("CorsFilter");
        filterRegistrationBean.addUrlPatterns("*.do","*.api","*.notify");
        return filterRegistrationBean;
    }
}
